package com.interview.ujiuye;

/**
 * Copyright (C), 2018-2020
 * FileName: Test
 * Author:   kongfanyu
 * Date:     2020/11/2 15:14
 */
public class Test {

    private static class MessageLoop implements Runnable{
        public void run(){
            try {
                for (int i=0; i<3; i++){
                    Thread.sleep(1000);
                    System.out.println(i);
                }
            }catch (InterruptedException e){
                System.out.println("A Exception");
            }
        }
    }


    public static void main(String[] args) throws InterruptedException {
        long startTime = System.currentTimeMillis();
        Thread thread = new Thread(new MessageLoop());
        thread.start();
        System.out.println("X");
        while (thread.isAlive()){
            thread.join(1000);
            if ( (System.currentTimeMillis() - startTime) > 1000 && thread.isAlive() ){
                thread.interrupt();
                System.out.println("Y");
                thread.join();
                System.out.println("Z");
            }
        }
    }

}
